package com.east.service.fee.impl;

import java.util.Date;
import java.util.List;
import java.util.UUID;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.east.bean.DictionaryItems;
import com.east.bean.Fee;
import com.east.bean.Income;
import com.east.bean.OtherFee;
import com.east.bean.Project;
import com.east.bean.Staff;
import com.east.controller.fee.info.Subject;
import com.east.dao.DictionaryItemsMapper;
import com.east.dao.IncomeMapper;
import com.east.dao.OtherFeeMapper;
import com.east.dao.ProjectMapper;
import com.east.dao.StaffMapper;
import com.east.exception.DescribeException;
import com.east.service.fee.OtherFeeService;
import com.east.utils.DateUtils;
import com.east.utils.Pagenation;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
@Service("otherFeeService")
public class OtherFeeServiceImpl  implements OtherFeeService{
	@Autowired
	OtherFeeMapper otherFeeMapper;
	@Autowired 
	StaffMapper staffMapper;
	@Autowired 
	ProjectMapper projectMapper;
	@Autowired 
	DictionaryItemsMapper dictionaryItemsMapper;
	@Override
	public Page<OtherFee> findByPage(Pagenation pagenation, String key,String subject,String startTime ,String endTime) {
		Page<OtherFee> startPage = PageHelper.startPage(pagenation.getPageNum(), pagenation.getPageSize());
		if(StringUtils.isBlank(endTime)) {
			endTime= DateUtils.getDaysOfDate(new Date());
		}
		otherFeeMapper.finByOtherFeeAll(key,subject,startTime,endTime);
		return startPage;
	}

	@Override
	public boolean saveOtherFee(OtherFee otherFee) {
		OtherFee ot = new OtherFee();
		ot.setOfAcount(otherFee.getOfAcount());//金额
		ot.setOfMan(otherFee.getOfMan());//经办人
		if(StringUtils.isNotBlank(otherFee.getDateTime())) {
			ot.setOfPayTime(DateUtils.getDate(otherFee.getDateTime()));//时间
		}
		ot.setOfSummary(otherFee.getOfSummary());//费用摘要
		ot.setOfType(otherFee.getOfType());//付款类型
		ot.setOfUuid(UUID.randomUUID().toString());
		ot.setProjectId(otherFee.getProjectId());
		ot.setStaffId(otherFee.getStaffId());
		
		int insert = otherFeeMapper.insert(ot);
		if(insert>0) {
			return true;
		}
		
		return false;
	}

	@Override
	public boolean updateOtherFee(OtherFee otherFee) {
		OtherFee ot = otherFeeMapper.selectByPrimaryKey(otherFee.getOfId());
		ot.setOfAcount(otherFee.getOfAcount());//金额
		ot.setOfMan(otherFee.getOfMan());//经办人
		if(StringUtils.isNotBlank(otherFee.getDateTime())) {
			ot.setOfPayTime(DateUtils.getDate(otherFee.getDateTime()));//时间
		}
		ot.setOfSummary(otherFee.getOfSummary());//费用摘要
		ot.setOfType(otherFee.getOfType());//付款类型
		ot.setProjectId(otherFee.getProjectId());
		ot.setStaffId(otherFee.getStaffId());
		int insert = otherFeeMapper.updateByPrimaryKey(ot);
		if(insert>0) {
			return true;
		}
		return false;
	}

	@Override
	public boolean delOtherFee(OtherFee otherFee) {
		int selectByPrimaryKey = otherFeeMapper.deleteByPrimaryKey(otherFee.getOfId());
		if(selectByPrimaryKey>0) {
			return true;
		}
		return false;
	}

	@Override
	public OtherFee findByOne(OtherFee otherFee) {
		return otherFeeMapper.selectByPrimaryKey(otherFee.getOfId());
	}
	
	@Override
	public String saveListOtherFee(List<OtherFee> fee) {
		for (OtherFee fe : fee) {
			fe.setOfUuid(UUID.randomUUID().toString());
			try {
				if(StringUtils.isNotBlank( fe.getStaffName())) {
					Staff findByName = staffMapper.findByName(fe.getStaffName());//经办人
					fe.setStaffId(findByName.getsId());
				}
			} catch (DescribeException e) {
				new DescribeException("经办人重名，请联系人事部门！！");
				return "经办人重名，请联系人事部门！！";
			}
			try {
				if(StringUtils.isNotBlank(  fe.getSubjectName())) {
				DictionaryItems findByName = dictionaryItemsMapper.findByName( fe.getSubjectName());//科目名称
				fe.setOfMan(findByName.getDiId());
				}
			} catch (DescribeException e) {
				new DescribeException("科目重复,请查看基本设置科目管理");
				return "科目重复,请查看基本设置科目管理";
			}
			
			try {
				if(StringUtils.isNotBlank( fe.getProjectNum())) {
					Project findByName = projectMapper.findBySerialNum( fe.getProjectNum());//科目名称
					fe.setProjectId(findByName.getpId());
				}
			} catch (DescribeException e) {
				new DescribeException("项目编号不唯一,请查看基本设置科目管理");
				return "项目编号不唯一,请查看基本设置科目管理";
			}
			try {
				if(StringUtils.isNotBlank(fe.getOfType())) {
				DictionaryItems findByName = dictionaryItemsMapper.findByName(fe.getOfType());//付款类型
				fe.setOfType(findByName.getDiId().toString());
				}
			} catch (DescribeException e) {
				new DescribeException("付款类型重复,请查看基本设置付款类型");
				return "付款类型重复,请查看基本设置付款类型";
			}
		}
		int insertList = otherFeeMapper.insertList(fee);
		if(insertList>0) {
			return "添加成功";
		}else {
			return "添加失败";
		}
		
	}
	
	@Override
	public List<Subject> fingBySubject(String year, String month) {
		return otherFeeMapper.findBySubject(year, month);
	}

}
